package io.renren.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.renren.dao.CustomSettlementDao;
import io.renren.dao.SocialSecurityPayDetailDao;
import io.renren.entity.CustomSettlementEntity;
import io.renren.entity.SocialSecurityAreaBasicEntity;
import io.renren.entity.SocialSecurityPayDetailEntity;
import io.renren.utils.ReflectUtil;
import io.renren.utils.SocialSecurityAreaBasicUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.dao.SocialSecurityPersonBasicDao;
import io.renren.entity.SocialSecurityPersonBasicEntity;
import io.renren.service.SocialSecurityPersonBasicService;


@Service("socialSecurityPersonBasicService")
public class SocialSecurityPersonBasicServiceImpl implements SocialSecurityPersonBasicService {
    @Autowired
    private SocialSecurityPersonBasicDao socialSecurityPersonBasicDao;

    @Autowired
    private SocialSecurityPayDetailDao socialSecurityPayDetailDao;

    @Autowired
    private CustomSettlementDao customSettlementDao;

    @Override
    public SocialSecurityPersonBasicEntity queryObject(Integer id) {
        return socialSecurityPersonBasicDao.queryObject(id);
    }

    @Override
    public List<SocialSecurityPersonBasicEntity> queryList(Map<String, Object> map) {
       // Calendar ca = Calendar.getInstance();
       // int year = ca.get(Calendar.YEAR);
      //  map.put("year", year);
        return socialSecurityPersonBasicDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return socialSecurityPersonBasicDao.queryTotal(map);
    }

    @Override
    public void save(SocialSecurityPersonBasicEntity socialSecurityPersonBasic) {
        socialSecurityPersonBasicDao.save(socialSecurityPersonBasic);
    }

    @Override
    public void update(SocialSecurityPersonBasicEntity socialSecurityPersonBasic) {
        socialSecurityPersonBasicDao.update(socialSecurityPersonBasic);
    }

    @Override
    public void delete(Integer id) {
        socialSecurityPersonBasicDao.delete(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        socialSecurityPersonBasicDao.deleteBatch(ids);
    }

    @Override
    public void updatePersonSocialSecurity(JSONArray jsonArray) {
        Calendar ca = Calendar.getInstance();
        int curyear = ca.get(Calendar.YEAR);

        JSONObject json = (JSONObject) jsonArray.get(0);
        SocialSecurityPersonBasicEntity pb = json.toJavaObject(SocialSecurityPersonBasicEntity.class);
        int year = Integer.valueOf(pb.getYear());
        int month = Integer.valueOf(pb.getMonth());
        String settlementid = pb.getSettlementid();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("year", year);
        map.put("month", month);
        map.put("settlementid", settlementid);
        CustomSettlementEntity customSettlementEntity = customSettlementDao.queryObject(settlementid);
        String empbasenum = customSettlementEntity.getEmpBaseNum();
        for (Object o : jsonArray) {
            JSONObject obj = (JSONObject) o;
            SocialSecurityPersonBasicEntity ssDetail = obj.toJavaObject(SocialSecurityPersonBasicEntity.class);

            //把每种保险的详细信息列出来
            double yanglbasic = ssDetail.getYanglbxBasic()==null?0:ssDetail.getYanglbxBasic();
            double yangldwbl = ssDetail.getYanglbxDwbl()==null?0:ssDetail.getYanglbxDwbl();
            double yanglgrbl = ssDetail.getYanglbxGrbl()==null?0:ssDetail.getYanglbxGrbl();
            double yilbasic = ssDetail.getYilbxBasic()==null?0:ssDetail.getYilbxBasic();
            double yildwbl = ssDetail.getYilbxDwbl()==null?0:ssDetail.getYilbxDwbl();
            double yilgrbl = ssDetail.getYilbxGrbl()==null?0:ssDetail.getYilbxGrbl();
            double shiybasic = ssDetail.getShiybxBasic()==null?0:ssDetail.getShiybxBasic();
            double shiydwbl = ssDetail.getShiybxDwbl()==null?0:ssDetail.getShiybxDwbl();
            double shiygrbl = ssDetail.getShiybxGrbl()==null?0:ssDetail.getShiybxGrbl();
            double gongsbasic = ssDetail.getGongsbxBasic()==null?0:ssDetail.getGongsbxBasic();
            double gongsdwbl = ssDetail.getGongsbxDwbl()==null?0:ssDetail.getGongsbxDwbl();
            double gongsgrbl = ssDetail.getGongsbxGrbl()==null?0:ssDetail.getGongsbxGrbl();
            double shengybasic = ssDetail.getShengybxBasic()==null?0:ssDetail.getShengybxBasic();
            double shengydwbl = ssDetail.getShengybxDwbl()==null?0:ssDetail.getShengybxDwbl();
            double shengygrbl = ssDetail.getShengybxGrbl()==null?0:ssDetail.getShengybxGrbl();
            double totalbasic = ssDetail.getTotalBasic()==null?0:ssDetail.getTotalBasic();
            double totaldwbl = ssDetail.getTotalDwbl()==null?0:ssDetail.getTotalDwbl();
            double totalgrbl = ssDetail.getTotalGrbl()==null?0:ssDetail.getTotalGrbl();

            Map<String, Object> params = new HashMap<>();
            params.put("settlementid", settlementid);
            params.put("empid",ssDetail.getEmpid());
            for (int i = year; i <= curyear; i++) {
                if (i == year) {
                    for (int j = month; j <= 12; j++) {
                        params.put("year", i);
                        params.put("month", j);
                        SocialSecurityPersonBasicEntity ss = socialSecurityPersonBasicDao.getSocialSecurityPersonBasicInfo(params);
                        if(ss != null){
                            ss = (SocialSecurityPersonBasicEntity) ReflectUtil.reflectClass(ssDetail,ss);
                            //ss = reflectSSPersonBasic(ssDetail,ss);
                            socialSecurityPersonBasicDao.update(ss);
                        }
                    }
                } else {
                    for (int j = 1; j <= 12; j++) {
                        params.put("year", i);
                        params.put("month", j);
                        SocialSecurityPersonBasicEntity ss = socialSecurityPersonBasicDao.getSocialSecurityPersonBasicInfo(params);
                        if(ss != null){
                            ss = (SocialSecurityPersonBasicEntity) ReflectUtil.reflectClass(ssDetail,ss);
                            //ss = reflectSSPersonBasic(ssDetail,ss);
                            socialSecurityPersonBasicDao.update(ss);
                        }
                    }
                }
            }

            int empid = ssDetail.getEmpid();
            //根据结算点编号,员工编号获取所有结算点字段值为该结算点编号的社保缴付明细表记录
            map.put("empid", empid);
            List<SocialSecurityPayDetailEntity> payDetailList = socialSecurityPayDetailDao.getPayDetailBySettlementid(map);
            for (SocialSecurityPayDetailEntity payDetail : payDetailList) {
                /**
                 * 如果是已经有付款记录的社保明细记录，即表明该记录为已经结算过的记录，
                 * 就修改补差字段的数据，同时修改补差缴付状态为未缴付
                 */
                SocialSecurityAreaBasicUtils.updatePayDetail(payDetail, yanglbasic, yangldwbl, yanglgrbl,
                        shengybasic, shengydwbl, shengygrbl, gongsbasic, gongsdwbl, gongsgrbl,
                        yilbasic, yildwbl, yilgrbl, shiybasic, shiydwbl, shiygrbl,
                        totalbasic, totaldwbl, totalgrbl, empbasenum);
                socialSecurityPayDetailDao.update(payDetail);
            }
        }
    }



}
